import java.util.Comparator;
import java.util.Objects;
import java.util.PriorityQueue;

/**
 * @User: vitobo
 * @Date: 2024-06-27
 * @Description: Java对象的比较 学习
 */
//class Card implements Comparable<Card> {
//    public int rank;
//    public String suit;
//
//    public Card(int rank, String suit){
//        this.rank = rank;
//        this.suit = suit;
//    }
//
//    @Override
//    public boolean equals(Object o) {
//        if (this == o) return true;
//        if (o == null || getClass() != o.getClass()) return false;
//        Card card = (Card) o;
//        return rank == card.rank && Objects.equals(suit, card.suit);
//    }
//
//    @Override
//    public int hashCode() {
//        return Objects.hash(rank, suit);
//    }
//
//
//    @Override
//    public int compareTo(Card o) {
//        if(o == null){
//            return 1;
//        }
//        return this.rank - o.rank;
//    }
//}
class Card {
    public int rank; // 数值
    public String suit; // 花色
    public Card(int rank, String suit) {
        this.rank = rank;
        this.suit = suit;
    }
}

class CardComparator implements Comparator<Card>{

    @Override
    public int compare(Card o1, Card o2) {
        if (o1 == o2) {
            return 0;
        }
        if (o1 == null) {
            return -1;
        }
        if (o2 == null) {
            return 1;
        }
        return o1.rank - o2.rank;
    }
}

public class TestCompare {

    public static void main(String[] args){
        Card p = new Card(1, "♠");
        Card q = new Card(2, "♠");
        Card o = new Card(1, "♠");
        // 定义比较器对象
        CardComparator cmptor = new CardComparator();
        // 使用比较器对象进行比较
        System.out.println(cmptor.compare(p, o)); // == 0，表示牌相等
        System.out.println(cmptor.compare(p, q)); // < 0，表示 p 比较小
        System.out.println(cmptor.compare(q, p)); // > 0，表示 q 比较大
    }

    public static void main1(String[] args) {
//        PriorityQueue<Card> p = new PriorityQueue<>();
//        p.offer(new Card(1, "♣"));
//        p.offer(new Card(2, "♠"));
        Card c1 = new Card(1, "♣");
        Card c2 = new Card(2, "♠");
        Card c3 = c1;

//        // == 调用equals方法, 直接比较的是 两个对象的地址
//        System.out.println(c1 == c2);
//        System.out.println(c1 == c3);

        //System.out.println(c1.compareTo(c2));

    }
}
